﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Org.BouncyCastle.Crypto.Tls;
using SocketEx;
using System.IO;
using System.Diagnostics;
using System.Net.Sockets;
using System.Windows.Threading;


namespace MoMoTransferVN_WP
{
    public partial class MainPage : PhoneApplicationPage
    {

        public const int STATUS_DEF = -1;
        public const int STATUS_INIT = 0;
        public const int STATUS_CONNECTING = 1;
        public const int STATUS_CONNECTED = 2;
        public const int STATUS_DISCONNECTED = 3;
        public const int STATUS_RECONNECTING = 4;

        //public const string SERVER_IP = "app.momo.vn";
        public const string SERVER_IP = "10.10.23.214";
        public const int SERVER_PORT = 8443;

        public int mStatus = STATUS_INIT;

        public SocketClient mClient;
        
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, RoutedEventArgs e)
        {
            handleConnect(sender, e);
        }

        private void handleConnect(object sender, RoutedEventArgs e)
        {
            mClient = new SocketClient();

            while (true)
            {
                switch (mStatus)
                {
                    case STATUS_DEF:
                        //Debug.WriteLine("status is def");
                        //todo nothing
                        break;
                    case STATUS_INIT:
                        Debug.WriteLine("status is init");

                        mStatus = STATUS_CONNECTING;
                        break;
                    case STATUS_CONNECTING:
                        Debug.WriteLine("status is connecting");

                        string result = mClient.Connect(SERVER_IP, SERVER_PORT);
                        if (!result.Equals(System.Net.Sockets.SocketError.Success.ToString()))
                        {
                            Debug.WriteLine("connect error with " + result + " need to reconnecting");
                            mStatus = STATUS_RECONNECTING;
                        }
                        else
                        {
                            mStatus = STATUS_CONNECTED;
                        }
                        break;
                    case STATUS_RECONNECTING:
                        Debug.WriteLine("status is reconnecting");

                        //mStatus = STATUS_DEF;
                        reconnect(sender, e);

                        break;

                    case STATUS_CONNECTED:
                        Debug.WriteLine("status is connected");
                        //stop timer
                        stopTimer();
                        
                        //todo send data when ready connected to server
                        sendHello(mClient, MoMoReq.processHello());

                        mClient.Receive();

                        //change status is default
                        mStatus = STATUS_DEF;

                        break;
                    case STATUS_DISCONNECTED:
                        Debug.WriteLine("status is disconnected");

                        mStatus = STATUS_CONNECTING;
                        break;
                }
            }
        }

        private void reconnect(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("reconnect after 1000ms");
            startTimer(sender, e);
        }

        private void sendHello(SocketClient mClient, MomoMessage msg)   
        {
            string result = mClient.Send(msg);
            Debug.WriteLine("send is " + result);
        }


        System.Windows.Threading.DispatcherTimer mDispatcherTimer;
        public void startTimer(object o, RoutedEventArgs sender)
        {
            mDispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            mDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000); // 1000 Milliseconds 
            mDispatcherTimer.Tick += new EventHandler(Each_Tick);
            mDispatcherTimer.Start();
        }

        public void stopTimer()
        {
            if (mDispatcherTimer != null)
                mDispatcherTimer.Stop();
        }


        // A variable to count with.
        int i = 0;

        // Raised every 1000 miliseconds while the DispatcherTimer is active.
        public void Each_Tick(object o, EventArgs sender)
        {
            //change status to conneting
            mStatus = STATUS_CONNECTING;

            i++;
            Debug.WriteLine("count = " + i);

        }
    }
}